<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib uri="http://eureka.yigolden.net/stringutils/sutaglib/1.0" prefix="su"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
<link type="text/css" href="${staticResources}/css/register.css" rel="stylesheet" />
<script type="text/javascript" src="${staticResources}/jquery/jquery-1.5.1.js"></script>
<script type="text/javascript" src="${staticResources}/js/register.js"></script>
<script type="text/javascript" src="${staticResources}/js/ajax.js"></script>
<script type = "text/javascript">
function refreshcaptcha()
{
	var timenow = new Date().getTime();    
	var captcha = document.getElementById("imgverifycode").src = "${contextPath}/captcha?flag=regu&random="+timenow;
}

function check()
{
	document.getElementById("nicknameerror").style.cssText="display:none" ;
	
	var nickname = document.getElementById("nickname").value;
	var redirect_url = document.getElementById("redirect_url").value;
	var mode = "ajax";	
	
	if(nickname.length==0)
	{
		document.getElementById("nicknameerror").innerHTML = "昵称不能为空！";
		document.getElementById("nicknameerror").style.cssText="display:inline" ;
		return;
	}
	
	var params="nickname=" + encodeURIComponent(nickname);
	params+="&redirect_url="+encodeURIComponent(redirect_url);
	params+="&mode="+encodeURIComponent(mode);
	
	doAjax("CheckNickNameExistence"
			,params,function(rst){
		var rst=eval('(' +rst+ ')');
		if (rst.status == 0)
		{
			document.getElementById("nicknameerror").style.cssText="display:inline;background:url('${staticResources}/image/tb.jpg')no-repeat;border:none;" ;
		}
		else if (rst.status != 0)
		{
			document.getElementById("nicknameerror").innerHTML = rst.message;
			document.getElementById("nicknameerror").style.cssText="display:inline" ;
		}
		return;
	 });
}

function register() {
	var staffid = document.getElementById("staffid").value;
	var realname = document.getElementById("realname").value;
	var idnumber = document.getElementById("idnumber").value;
	var nickname = document.getElementById("nickname").value;
	var password = document.getElementById("password").value;
	var repassword = document.getElementById("repassword").value;
	var tele = document.getElementById("tele").value;
	var email = document.getElementById("email").value;
	var verifycode = document.getElementById("verifycode").value;
	var redirect_url = document.getElementById("redirect_url").value;
	var mode = "ajax";
	
	var error=0;
	if(staffid.length==0)
	{
		document.getElementById("staffiderror").innerHTML = "学号不能为空！";
		document.getElementById("staffiderror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(realname.length==0)
	{
		document.getElementById("realnameerror").innerHTML = "姓名不能为空！";
		document.getElementById("realnameerror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(idnumber.length==0)
	{
		document.getElementById("idnumbererror").innerHTML = "身份证号不能为空！";
		document.getElementById("idnumbererror").style.cssText="display:inline" ;
		error=error+1;
	}
	else if(idnumber.length!=18)
	{
		document.getElementById("idnumbererror").innerHTML = "您输入的身份证号不正确！";
		document.getElementById("idnumbererror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(nickname.length==0)
	{
		document.getElementById("nicknameerror").innerHTML = "昵称不能为空！";
		document.getElementById("nicknameerror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(password.length==0)
	{
		document.getElementById("passworderror").innerHTML = "密码不能为空！";
		document.getElementById("passworderror").style.cssText="display:inline" ;
		error=error+1;
	}
	else if(password.length<6||password.length>20)
	{
		document.getElementById("passworderror").innerHTML = "密码长度必须在6~20之间！";
		document.getElementById("passworderror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(repassword.length==0)
	{
		document.getElementById("repassworderror").innerHTML = "确认密码不能为空！";
		document.getElementById("repassworderror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(password.value!=repassword.value)
	{
		document.getElementById("repassworderror").innerHTML = "两次输入不一致！";
		document.getElementById("repassworderror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(tele.length==0)
	{
		document.getElementById("teleerror").innerHTML = "手机号不能为空！";
		document.getElementById("teleerror").style.cssText="display:inline" ;
		error=error+1;
	}
	else if(tele.length!=11)
	{
		document.getElementById("teleerror").innerHTML = "您输入的手机号不正确！";
		document.getElementById("teleerror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(email.length==0)
	{
		document.getElementById("emailerror").innerHTML = "邮箱不能为空！";
		document.getElementById("emailerror").style.cssText="display:inline" ;
		error=error+1;
	}
	
	if(verifycode.length==0)
	{
		document.getElementById("verifycodeerror").innerHTML = "验证码不能为空！";
		document.getElementById("verifycodeerror").style.cssText="display:inline" ;
		error=error+1;
	}
	else if(verifycode.length!=4)
	{
		document.getElementById("verifycodeerror").innerHTML = "您输入的验证码不正确！";
		document.getElementById("verifycodeerror").style.cssText="display:inline" ;
		error=error+1;
	}
	if(error!=0)
		return;
	
	var params="staffid=" + encodeURIComponent(staffid);
	params+="&realname="+encodeURIComponent(realname);
	params+="&idnumber="+encodeURIComponent(idnumber);
	params+="&nickname="+encodeURIComponent(nickname);
	params+="&password="+encodeURIComponent(password);
	params+="&tele="+encodeURIComponent(tele);
	params+="&email="+encodeURIComponent(email);
	params+="&verifycode="+encodeURIComponent(verifycode);
	params+="&redirect_url="+encodeURIComponent(redirect_url);
	params+="&mode="+encodeURIComponent(mode);

	doAjax("Register"
			,params,function(rst){
		var rst=eval('(' +rst+ ')');
		if (rst.status == 0)
		{
			location.href = rst.redirectURL;
		}
		else if (rst.status == 1)
		{
			document.getElementById("staffiderror").innerHTML = rst.message;
			document.getElementById("staffiderror").style.cssText="display:inline" ;
		}
		else if (rst.status == 2)
		{
			document.getElementById("nicknameerror").innerHTML = rst.message;
			document.getElementById("nicknameerror").style.cssText="display:inline" ;
		}
		else if (rst.status == 3)
		{
			document.getElementById("idnumbererror").innerHTML = rst.message;
			document.getElementById("idnumbererror").style.cssText="display:inline" ;
		}
		else if(rst.status == 4)
		{
			document.getElementById("verifycodeerror").innerHTML = rst.message;
			document.getElementById("verifycodeerror").style.cssText="display:inline" ;
		}
		return;
	 });
}
</script>
</head>
<body>

<div id="divmain">
  <div id="divtitle">
    <img src="${staticResources}/image/register_1.jpg" width=880px; id="imgtitle"/>
  </div>
  <div id="divbody">
  <img src="${staticResources}/image/register_2.jpg" width=800px; id="imgtitle"/>
  <form method="post" action="Register" onsubmit="register();return false;">
    <table id="tableform">
      <tr>
        <td class="tdtext">学号：</td>
        <td class="tdinput"> <input class="inputclass" type="text" name="staffid" id="staffid"> </td>
        <td class="tderror"> <label class="errorclass" id="staffiderror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">姓名：</td>
        <td class="tdinput">  <input class="inputclass" type="text" name="realname" id="realname"> </td>
        <td class="tderror"> <label class="errorclass" id="realnameerror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">身份证号：</td>
        <td class="tdinput"> <input class="inputclass" type="text" name="idnumber" id="idnumber"> </td>
        <td class="tderror"> <label class="errorclass" id="idnumbererror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">昵称：</td>
        <td class="tdinput"> <input class="inputclass" type="text" name="nickname" id="nickname" onblur="check()"> </td>
        <td class="tderror"> <label class="errorclass" id="nicknameerror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">密码：</td>
        <td> <input class="inputclass" type="password" name="password" id="password"> </td>
        <td> <label class="errorclass" id="passworderror"></label>  </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">确认密码：</td>
        <td> <input class="inputclass" type="password" name="repassword" id="repassword"> </td>
        <td> <label class="errorclass" id="repassworderror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">手机号：</td>
        <td class="tdinput"> <input class="inputclass" type="text" name="tele" id="tele"> </td>
        <td class="tderror"> <label class="errorclass" id="teleerror"></label>
        </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">Email：</td>
        <td> <input class="inputclass" type="text" name="email" id="email"> </td>
        <td> <label class="errorclass" id="emailerror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext">验证码：</td>
        <td> <input class="inputclass" type="text" name="verifycode" id="verifycode"> </td>
        <td> <label class="errorclass" id="verifycodeerror"></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext"></td>
        <td> <div id="divverifycode"><img id="imgverifycode" src="${contextPath}/captcha?flag=regu"/></div></td>
        <td> <label><button onclick = "refreshcaptcha();return false;">换一张</button></label> </td>
      </tr>
      <tr height=10px></tr>
      
      <tr>
        <td class="tdtext"></td>
        <td> <input type="image" src="${staticResources}/image/register_3%20.jpg" class="button" name="commit" id="submitbtn"/> </td>
        <td> <label></label></td>
      </tr>
      <tr height=10px></tr>
    </table>
    <input type="hidden" name="redirect_url" id="redirect_url" value="${su:encodeLink(param.redirect_url) }" />
  </form>
  </div>  
</div>

</body>
</html>